/**
 * @author hfutrell
 */

// ueses sylvester matrix functions

function matrixstack() {
	this.matrix = Matrix.I(4);
	this.stack = [];
	//this.psMatrix = makePerspective();
}

matrixstack.prototype.matrix = null;
//matrixstack.prototype.psMatrix = null;

matrixstack.prototype.loadIdentity = function() {
    this.matrix = Matrix.I(4);
};

matrixstack.prototype.multMatrix = function(m) {
    this.matrix = this.matrix.x(m);
};

matrixstack.prototype.rotate = function(amt, axis) {
	var rotationMatrix = Matrix.Rotation(amt * Math.PI / 180.0, $V([axis[0], axis[1], axis[2]])).ensure4x4();
	this.multMatrix(rotationMatrix);
}

matrixstack.prototype.translate = function(v) {
	var translation = Matrix.Translation($V([v[0], v[1], v[2]])).ensure4x4();
    this.multMatrix(translation);
};

matrixstack.prototype.loadMatrix = function(m) {
	this.matrix = m;
}

matrixstack.prototype.pushMatrix = function() {
	this.stack.push(this.matrix);
}

matrixstack.prototype.popMatrix = function() {
	if (this.stack.length > 0) {
		this.matrix = this.stack.pop();
	}
	else {
		console.warn("popping empty matrix stack");
	}
}

// these should be moved to a different file

matrixstack.prototype.setMatrixUniforms = function(shaderProgram, name){
    var pUniform = gl.getUniformLocation(shaderProgram, name);
    gl.uniformMatrix4fv(pUniform, false, new Float32Array(this.matrix.flatten()));
};